/**
 * Created by liulin on 14-3-26.
 */
var APP_PATH = '/';  //定义js中当前应用路径的全局变量
var PUBLIC=APP_PATH + 'public/';
var arrFRONT_USER_TYPES=['', '消费者', '设计师', '施工队长', '监理', '商户'];  //用户类型JS数组
var arrPageSizes=[10,20,30,40,50];
var MobileSMSExpirs=180; //短信过期时间 180秒

function get_datatable_pagesize_list(curr_pagesize){
    var datatable_pagesize_list= "<select>";
    for(var i=0;i<arrPageSizes.length;i++){
        if(curr_pagesize==arrPageSizes[i])
        {
            datatable_pagesize_list +="<option selected>" + arrPageSizes[i]  +"</option>";
        }else{
            datatable_pagesize_list +="<option>" + arrPageSizes[i]  +"</option>";
        }
    }
    datatable_pagesize_list += "</select>";
    return datatable_pagesize_list;
}

function changeform(parent,obj){
    $(parent).find('.idtabs a').removeClass('on');
    $(obj).addClass('on');
    $(parent).find('.tabs').hide();
    $(parent).find('#' + $(obj).attr('tab')).show();
}

function is_mobile(data){
    if(data!='' && (/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/).test(data)==true){
        return true;
    }else{
        return false;
    }
}

function is_mail(data){
    var regex=new RegExp("^[0-9a-z_\.-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$","i");
    if(data!='' && regex.test(data)==true){
        return true;
    }else{
        return false;
    }
}

function is_username(data){
    var regex=new RegExp("^[a-z0-9_]{5,}$","i");
    if(data!='' && regex.test(data)==true){
        return true;
    }else{
        return false;
    }
}


function MobileSMSSend(){
    this.Running =false;   //true -- running   false -- stop
    this.MobileObject='';   //手机号id
    this.StatusObject ='';   //状态更新对象
    this.StatusType ='tag';   //状态更新类型 tag为标签类型更新 通过html更新  text为表单控件更新 通过 val更新
    this.Expires =180;  //短信过期时间
    this.Timer =null;   //时钟对象
    this.ajax_url = ''; //发送短信的url
    this.data=null;     //发送的附加数据对象
    this.type="GET";    //发出请求的类型  GET POST
    this.initMessage='点击获取验证码';//初始字符串
    this.refreshMessage='短信已发送【second秒】';//更新字符串  second将被替换为当前秒值
    this.stopMessage='重新获取验证码';//停止字符串
    this.currValue=0;

    if(this.StatusType=='tag'){
        $(this.StatusObject).html(this.initMessage);
    }else{
        $(this.StatusObject).val(this.initMessage);
    }

    this.stopRefresh=function(){//停止函数
        if(this.StatusType=='tag'){
            $(this.StatusObject).html(this.stopMessage);
        }else{
            $(this.StatusObject).val(this.stopMessage);
        }
        clearInterval(this.Timer);
        this.currValue=this.Expires;
        this.Running=false;
    };

    this.refresh=function(){//更新函数
        if(!this.Running) return false;
        this.currValue--;
        if(this.currValue<=0){
            this.stopRefresh();
            return;
        }
        if(this.StatusType=='tag'){
            $(this.StatusObject).html(this.refreshMessage.replace('second', this.currValue));
        }else{
            $(this.StatusObject).val(this.refreshMessage.replace('second', this.currValue));
        }
    };


    this.send=function(){//发送函数
        if(this.Running){//已经发送
            return false;
        }

        if(is_mobile($(this.MobileObject).val())){
            //是手机才发送
            this.data.phone=$(this.MobileObject).val();
            $.get(this.ajax_url,this.data,this.callback);
        }
    };

    this.callback=function(data){
        if(data=='1'){
            alert(this.Expires);
            this.Running=true;
            this.currValue=this.Expires;
            this.Timer=setInterval(this.refresh,1000);
        }else{
            this.stopRefresh();
        }
    };

    return this;

}

/**
 * 对time描述的以秒为单位的整数时间转字符  yyyy-MM-dd HH:mm:ss
 * @param time  输入的以秒为单位的整数时间
 */
function intSeconds2date(time){
    var dd=new Date(time * 1000);
    var r = dd.getFullYear()  + '-' ;
    var m = dd.getMonth() + 1;
    if( m < 10){
        r += '0' + m + '-';
    }else{
        r += m + '-';
    }
    var d = dd.getDate();
    if( d < 10){
        r += '0' + d + ' ';
    }else{
        r += d + ' ';
    }
    var h = dd.getHours();
    if( h < 10){
        r += '0' + h + ':';
    }else{
        r += h + ':';
    }
    var i = dd.getMinutes();
    if( i < 10){
        r += '0' + i + ':';
    }else{
        r += i + ':';
    }
    var s = dd.getSeconds();
    if( s < 10){
        r += '0' + s ;
    }else{
        r += s ;
    }
    return r;
}

/**
 * 根据秒计算 天，小时，分，秒
 * @param seconds 秒
 */
function compute_time(use_seconds){
    var left_second=parseInt(use_seconds % 60);
    var use_minutes=parseInt(use_seconds / 60);
    var left_minute=use_minutes % 60;
    var use_hours=parseInt(use_minutes / 60);
    var left_hour=use_hours % 24;
    var use_days=parseInt(use_hours / 24);
    return [use_days,left_hour,left_minute,left_second];

}




function open_popup_window(divid){
    var screenW = $(window).width();
    var screenH = document.documentElement.clientHeight;
    var scrollH=document.documentElement.scrollTop;
    $("#"+divid).css({ left:(screenW - $("#"+divid).width())/2, top:(scrollH+screenH - $("#"+divid).height())/2 }).show();
}
function close_popup_window(divid){
    $('#'+divid).hide();
}

function initDataTablesScroll(){
    var u = navigator.userAgent, app = navigator.appVersion;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if(!isAndroid && !isiOS){
        $('.table-responsive').css({'overflow':'hidden'});
    }
}

function preview1(file) {
    var img = new Image(), url = img.src = URL.createObjectURL(file)
    var $img = $(img)
    img.onload = function() {
        URL.revokeObjectURL(url)
        $('#preview').empty().append($img)
    }
}

function preview2(obj,file) {
    var img = new Image(), url = img.src = URL.createObjectURL(file)
    var $img = $(img)
    img.onload = function() {
        URL.revokeObjectURL(url);
        $(obj).parent().find('.preview').empty().append($img)
    }
}